/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.RegistrarCanchaDao;
import edu.upc.clase.demo.entity.Cancha;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Omar
 */
@Repository
public class RegistrarCanchaDaoImpl extends SimpleJdbcDaoSupport implements RegistrarCanchaDao {

    private static Logger log = LoggerFactory.getLogger(RegistrarCanchaDaoImpl.class);

    @Autowired
    public RegistrarCanchaDaoImpl(DataSource dataSource) {
        log.info("Asignando el dataSource");
        setDataSource(dataSource);
    }

    @Override
    public Integer insertar(Cancha cancha) {
        getJdbcTemplate().update("insert into ZREGCANCHA(IDLOCAL, CCARACT, CNOMBRE) values (?,?,?)",
                cancha.getIDLOCAL(), cancha.getCCARACT(), cancha.getCNOMBRE());
        return getSimpleJdbcTemplate().queryForInt("call identity()");
    }

    @Override
    public Cancha buscar(Cancha cancha) {
    throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Cancha buscarPorNombreyLocal(Cancha cancha) {
         try {
            String Select = "select IDCAN, IDLOCAL,CNOMBRE,CCARACT from ZREGCANCHA"
                    + "where IDLOCAL = ? and CNOMBRE = ?";

            return (Cancha) getSimpleJdbcTemplate().query(Select,
                        new BeanPropertyRowMapper<Cancha>(Cancha.class),cancha.getIDLOCAL(), cancha.getCNOMBRE());

        } catch (EmptyResultDataAccessException e) {
            return null;
        }
    }
}
